<?php
/**
 * The current eclaire version.
 */
define('ECLAIRE_VERSION', '0.2');

/**
 * ecAutoload class.
 *
 */
class ecAutoload
{
  static protected
    $registered = false,
    $instance   = null;

  protected
    $baseDir = null;

  protected function __construct()
  {
    $this->baseDir = realpath(dirname(__FILE__).'/..');
  }

  static public function getInstance()
  {
    if (!isset(self::$instance))
    {
      self::$instance = new ecAutoload();
    }

    return self::$instance;
  }

  static public function register()
  {
    if (self::$registered)
    {
      return;
    }

    ini_set('unserialize_callback_func', 'spl_autoload_call');
    if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
    {
      throw new Exception(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
    }

    self::$registered = true;
  }

  static public function unregister()
  {
    spl_autoload_unregister(array(self::getInstance(), 'autoload'));
    self::$registered = false;
  }

  public function autoload($class)
  {
    if ($path = $this->getClassPath($class))
    {
      require $path;

      return true;
    }

    return false;
  }

  public function getClassPath($class)
  {
    $class = strtolower($class);

    if (!isset($this->classes[$class]))
    {
      return null;
    }

    return $this->baseDir.'/'.$this->classes[$class];
  }

  public function getBaseDir()
  {
    return $this->baseDir;
  }

  static public function make()
  {
    $libDir = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'));
    require_once $libDir.'/core/util/sfFinder.class.php';

    $files = sfFinder::type('file')
      ->prune('plugins')
      ->prune('vendor')
      ->prune('skeleton')
      ->prune('default')
      ->prune('helper')
      ->name('*.php')
      ->in($libDir)
    ;

    sort($files, SORT_STRING);

    $classes = '';
    foreach ($files as $file)
    {
      $file  = str_replace(DIRECTORY_SEPARATOR, '/', $file);
      $class = basename($file, false === strpos($file, '.class.php') ? '.php' : '.class.php');

      $contents = file_get_contents($file);
      if (false !== stripos($contents, 'class '.$class) || false !== stripos($contents, 'interface '.$class))
      {
        $classes .= sprintf("    '%s' => '%s',\n", strtolower($class), substr(str_replace($libDir, '', $file), 1));
      }
    }

    $content = preg_replace('/protected \$classes = array *\(.*?\);/s', sprintf("protected \$classes = array(\n%s  );", $classes), file_get_contents(__FILE__));

    file_put_contents(__FILE__, $content);
  }

  // Don't edit this property by hand.
  // To update it, use ecAutoload::make()
  protected $classes = array(
    'ecaction' => 'core/app/ecAction.class.php',
    'ecappconfiguration' => 'core/app/ecAppConfiguration.class.php',
    'eccontroller' => 'core/app/ecController.class.php',
    'dao' => 'core/dao/dao.class.php',
    'daostatement' => 'core/dao/daostatement.class.php',
    'db' => 'core/dao/db.class.php',
    'ecbase' => 'core/dao/ecBase.class.php',
    'ectracetemp' => 'core/dao/ecTraceTemp.class.php',
    'mysql' => 'core/dao/mysql.class.php',
    'pager' => 'core/dao/pager.class.php',
    'ecautoload' => 'core/ecAutoload.class.php',
    'ecconfiguration' => 'core/ecConfiguration.class.php',
    'ecconnectiondatabase' => 'core/ecConnectionDatabase.class.php',
    'ecenvironment' => 'core/ecEnvironment.class.php',
    'ecform' => 'core/form/ecForm.class.php',
    'ecformdb' => 'core/form/ecFormDb.class.php',
    'ecformelement' => 'core/form/ecFormElement.class.php',
    'eccheckbox' => 'core/form/elements/ecCheckBox.class.php',
    'ecdateinput' => 'core/form/elements/ecDateInput.class.php',
    'ecfileinput' => 'core/form/elements/ecFileInput.class.php',
    'echiddenfield' => 'core/form/elements/ecHiddenField.class.php',
    'ecmailinput' => 'core/form/elements/ecMailInput.class.php',
    'ecpasswordinput' => 'core/form/elements/ecPasswordInput.class.php',
    'ecradio' => 'core/form/elements/ecRadio.class.php',
    'ecselectlist' => 'core/form/elements/ecSelectList.class.php',
    'ectextinput' => 'core/form/elements/ecTextInput.class.php',
    'ectextareainput' => 'core/form/elements/ecTextareaInput.class.php',
    'echelper' => 'core/helpers/ecHelper.class.php',
    'eci18n' => 'core/i18n/ecI18n.class.php',
    'ecxmli18n' => 'core/i18n/ecXMLI18n.class.php',
    'ecroute' => 'core/routing/ecRoute.class.php',
    'ecrouter' => 'core/routing/ecRouter.class.php',
    'ecsystem' => 'core/system/ecSystem.class.php',
    'ectemplate' => 'core/templates/ecTemplate.class.php',
    'ectemplatehelpers' => 'core/templates/ecTemplateHelpers.class.php',
    'ectemplatejavascript' => 'core/templates/ecTemplateJavascript.class.php',
    'ectemplatestylesheets' => 'core/templates/ecTemplateStyleSheets.class.php',
    'ectemplatetaghelper' => 'core/templates/ecTemplateTagHelper.class.php',
    'ectemplateviewslayout' => 'core/templates/ecTemplateViewsLayout.class.php',
    'ectemplateviewsmodules' => 'core/templates/ecTemplateViewsModules.class.php',
    'date' => 'core/util/date.class.php',
    'phpmailer' => 'core/util/phpmailer.class.php',
    'rewrite' => 'core/util/rewrite.class.php',
    'secure' => 'core/util/secure.class.php',
    'sffilesystem' => 'core/util/sfFilesystem.class.php',
    'sffinder' => 'core/util/sfFinder.class.php',
    'sfinflector' => 'core/util/sfInflector.class.php',
    'sftoolkit' => 'core/util/sfToolkit.class.php',
    'smtp' => 'core/util/smtp.class.php',
    'upload' => 'core/util/upload.class.php',
    'validator' => 'core/util/validator.class.php',
    'sfyaml' => 'core/yaml/sfYaml.php',
    'sfyamldumper' => 'core/yaml/sfYamlDumper.php',
    'sfyamlinline' => 'core/yaml/sfYamlInline.php',
    'sfyamlparser' => 'core/yaml/sfYamlParser.php',
  );
}